<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>大事件-后台登录页</title>
  <link rel="stylesheet" type="text/css" href="css/reset.css" />
  <link rel="stylesheet" type="text/css" href="css/iconfont.css" />
  <link rel="stylesheet" type="text/css" href="css/main.css" />
  <!-- 引入bootstrap的css文件 -->
  <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
  <!-- 引入jq文件 -->
  <script src="./libs/jquery-1.12.4.min.js"></script>
  <!-- 引入bootstrap的js文件 -->
  <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
  <!-- 引入接口文件 -->
  <script src="./libs/https.js"></script>
</head>

<body>
  <div class="main_wrap">
    <div class="header">
      <a href="#" class="logo"><img src="images/logo.png" alt="" /></a>
      <div class="copyright">
        CopyRight © 2019 深圳百秀信息技术股份有限公司<br />All Rights Reserved
      </div>
    </div>

    <div class="login_form_con">
      <div class="login_title"></div>
      <form class="login_form">
        <i class="iconfont icon-user"></i>
        <i class="iconfont icon-key"></i>
        <input type="text" class="input_txt" placeholder="邮箱/手机号" />
        <input type="password" class="input_pass" placeholder="密码" />
        <input type="submit" class="input_sub" value="登 录" />
      </form>
    </div>
  </div>

  <!-- 模态框 -->
  <!-- 模态框静态结构 -->
  <div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
              aria-hidden="true">&times;</span></button>
          <h4 class="modal-title"></h4>上课认真听讲</h4>
        </div>
        <div class="modal-body">
          <p></p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default btn-success" data-dismiss="modal">确定</button>
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->

</body>

</html>

<script>
  $(function () {
    // 给后台登录按钮注册点击事件
    $('.input_sub').on('click', function (e) {
      // 1.阻止默认效果
      e.preventDefault();

      // 2.获取输入的用户名和密码
      let username = $('.input_txt').val().trim();
      let password = $('.input_pass').val().trim();

      // 3.非空判断
      if (username == '' || password == '') {
        // 不适用原生的弹窗（颜值低）
        // alert('用户名或密码不能为空，请重新输入用户名和密码');

        // 使用bootstrap插件弹窗
        $('#myModal').modal();
        $('.modal-body p').text('用户名或密码不能为空，请重新输入用户名和密码')
        $('#myModal').on('keydown', function (e) {
          if (e.keyCode == 13) {
            window.location = './login.html';
          }
        })
        return;
      }

      console.log(username);

      // 4.根据获取的数据发起ajax请求，跳转页面
      $.ajax({
        type: 'post',
        url: BigNew.user_login,
        data: {
          username: username,
          password: password
        },
        success: function (backData) {
          console.log(backData);
          if (backData.code == 200) {
            // 将返回的token保存到本地
            localStorage.setItem('bignews', backData.token)

            // alert(backData.msg); 原生js的弹窗可以卡主代码的执行

            $('#myModal').modal();
            $('.modal-body p').text('登录成功  欢迎回家！');
            $('#myModal').on('keydown', function (e) {
              if (e.keyCode == 13) {
                window.location = './index.html';
              }
            });

            // bootstrap的弹窗无法卡住代码，所以会直接跳转

            // 当弹窗被关闭的时候，再触发页面跳转
            $('#myModal').on('hidden.bs.modal', function () {
              window.location = './index.html'; // 跳转到后台管理界面
            })
          } else {
            $('#myModal').modal();
            $('.modal-body p').text('用户名或密码不正确，请重新输入用户名和密码');
            $('#myModal').on('keydown', function (e) {
              if (e.keyCode == 13) {
                window.location = './login.html';
              }
            })
          }
        }
      })
    })
  })
</script>